//
//  UIColor+RGBString.m
//  Camera
//
//  Created by 任雨婷 on 16/7/7.
//  Copyright © 2016年 任雨婷. All rights reserved.
//

#import "UIColor+RGBString.h"

@implementation UIColor (RGBString)

+ (UIColor *)colorWithRGBString:(NSString *)string alpha:(CGFloat)alpha{
    
    NSString *RGB = [string substringFromIndex:1];
    NSString *red = [RGB substringToIndex:2];
    NSString *green = [RGB substringWithRange:NSMakeRange(2, 2)];
    NSString *blue = [RGB substringFromIndex:4];
    CGFloat redFloat = [self floatWithString:red];
    CGFloat greenFloat = [self floatWithString:green];
    CGFloat blueFloat = [self floatWithString:blue];
    UIColor *color =  [UIColor colorWithRed:redFloat / 255.0 green:greenFloat / 255.0 blue:blueFloat / 255.0 alpha:alpha];
    return color;
}

+ (CGFloat)floatWithString:(NSString *)string {
    int tensTemp = 0;
    int unitsTemp = 0;
    CGFloat tensFloat = 0;
    CGFloat unitsFloat = 0;
    CGFloat colorFloat = 0;
    char first = [string characterAtIndex:0];
    char second = [string characterAtIndex:1];
    if (isupper(first)) {
        tensTemp = first - 55;
        tensFloat = tensTemp * 16;
    }else if (first >= 0 ||first <=9) {
        tensTemp = first - 48;
        tensFloat = tensTemp * 16;
    }
    
    if (isupper(second)) {
        unitsTemp = second - 55;
        unitsFloat = unitsTemp;
    }else if (second >= 0 ||second <=9) {
        unitsTemp = second - 48;
        unitsFloat = unitsTemp;
    }
    
    colorFloat = tensFloat + unitsFloat;
    return colorFloat;
}


- (CGColorSpaceModel) colorSpaceModel {
    return CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
}

- (NSString *) colorSpaceString {
    switch ([self colorSpaceModel])
    {
        case kCGColorSpaceModelUnknown:
            return @"kCGColorSpaceModelUnknown";
        case kCGColorSpaceModelMonochrome:
            return @"kCGColorSpaceModelMonochrome";
        case kCGColorSpaceModelRGB:
            return @"kCGColorSpaceModelRGB";
        case kCGColorSpaceModelCMYK:
            return @"kCGColorSpaceModelCMYK";
        case kCGColorSpaceModelLab:
            return @"kCGColorSpaceModelLab";
        case kCGColorSpaceModelDeviceN:
            return @"kCGColorSpaceModelDeviceN";
        case kCGColorSpaceModelIndexed:
            return @"kCGColorSpaceModelIndexed";
        case kCGColorSpaceModelPattern:
            return @"kCGColorSpaceModelPattern";
        default:
            return @"Not a valid color space";
    }
}

- (CGFloat) red {
    const CGFloat *c = CGColorGetComponents(self.CGColor);
    return c[0];
}

- (CGFloat) green {
    const CGFloat *c = CGColorGetComponents(self.CGColor);
    if ([self colorSpaceModel] == kCGColorSpaceModelMonochrome) return c[0];
    return c[1];
}

- (CGFloat) blue {
    const CGFloat *c = CGColorGetComponents(self.CGColor);
    if ([self colorSpaceModel] == kCGColorSpaceModelMonochrome) return c[0];
    return c[2];
}

- (CGFloat) alpha {
    const CGFloat *c = CGColorGetComponents(self.CGColor);
    return c[CGColorGetNumberOfComponents(self.CGColor)-1];
}

@end
